const state = {
  Authorization: null,
  studentNumber: '',
  tower: '',
  dorm: ''
}

const mutations = {
  changeLogin (state, payload) {
    state.Authorization = payload
    window.localStorage.setItem('Authorization', payload)
  },
  loginOut (state, payload) {
    state.Authorization = payload
    state.studentNumber = payload
  },
  addToStudent (state, payload) {
    state.studentNumber = payload.studentNum
    window.localStorage.setItem('studentNumber', payload.studentNum)
    window.localStorage.setItem('name', payload.username)
  },
  addToTowerAndDorm (state, payload) {
    state.tower = payload.tower
    state.dorm = payload.dorm
    window.localStorage.setItem('tower', payload.tower)
    window.localStorage.setItem('dorm', payload.dorm)
  }
}
const actions = {
  // 添加token到state前判断state中有没有token
  addTokenToState (context, payload) {
    if (context.state.Authorization === null) {
      // 没有就将token给存入state
      context.commit('changeLogin', payload)
    } else {
      // 判断state中的token是否和state中的是否一致
      if (context.state.Authorization === payload) {
        console.log('已存在')
      }
    }
  },
  addStudentNumber (context, user) {
    if (!context.studentNumber) {
      context.commit('addToStudent', user)
    } else {
      if (context.state.studentNumber === user.studentNum) {
        console.log('已存在')
      }
    }
  },
  addTowerAndDorm (context, payload) {
    if (!context.state.tower || !context.dorm) {
      context.commit('addToTowerAndDorm', payload)
    } else {
      console.log('已存在')
    }
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
